#ifndef PROTOCOL_H
#define PROTOCOL_H

#include "src/define/data_define.h"
#include "math.h"

/// 定义通讯的类型
typedef enum Tag_PROTOCOL_DATA
{
    T_P_HEADER = 0xFF,
    T_P_GET    =0x01,           /// 与bit0进行&操作,得出校验位
    T_P_CHECK  = 0xFF,          /// 获取UC2的低位字节
    T_P_DATA_LENTH  = 21,       /// 数据帧有效位
    T_P_3D_RANGE    = 0x400 ,   /// 3D摇杆的航程 ,1024 - 3ff+1
    T_P_3D_MID      = 0x200,    /// 3D摇杆的中心  0512 - 1ff+1
    T_P_ROBOT_RANGE = 200,       /// 单次单方向的最大航程
    T_P_QUEQUE_MAX  = 10,       /// 当队列大于10次，进行平滑处理
    T_P_SPEED_GEAR  = 10,        /// dang wei
    T_P_SPEED_MAX   = 568,
    // T_P_ONCE_MAX    = 500,       /// 500mm/s
    T_P_ALL
}T_PROTO_D;




/// 定义通讯的索引
typedef enum Tag_PROTOCOL_INDEX
{
    T_I_HEADER = 0,
    T_I_XH, /// X
    T_I_XL,
    T_I_YH, /// Y
    T_I_YL,
    T_I_ZH, /// Z
    T_I_ZL,
    T_I_TH, /// 速度切换
    T_I_TL,
    T_I_AH, ///
    T_I_AL,
    T_I_BH, ///
    T_I_BL,
    T_I_CH, ///
    T_I_CL,
    T_I_DH, ///
    T_I_DL,
    T_I_B1, /// 按钮组 1-8
    T_I_B2, /// 按钮组 9-16
    T_I_B3, /// 按钮组 17-20
    T_I_CHECK,
    T_I_ALL
}T_PROTO_I;

/// 定义button索引
typedef enum Tag_BUTTON_INDEX
{
    T_B_BTN1 = 0,
    T_B_BTN2,
    T_B_BTN3,
    T_B_BTN4,
    T_B_BTN5,
    T_B_BTN6,
    T_B_BTN7,
    T_B_BTN8,
    T_B_BTN9  = 0,
    T_B_BTN10,
    T_B_BTN11,
    T_B_BTN12,
    T_B_BTN13,
    T_B_BTN14,
    T_B_BTN15,
    T_B_BTN16,
    T_B_BTN17  = 0,
    T_B_BTN18,
    T_B_BTN19,
    T_B_BTN20,
    T_B_ALL
}T_BUTTON_I;


/// 按键索引
typedef struct ST_BTN_INDEX {
    T_BUTTON_I _B_CRITICAL  = T_B_BTN3; /// 急停
    T_BUTTON_I _B_OUT       = T_B_BTN7; /// 送
    T_BUTTON_I _B_IN        = T_B_BTN6; /// 收
    T_BUTTON_I _B_PAUSE     = T_B_BTN4; /// 暂停
    T_BUTTON_I _B_EN        = T_B_BTN5; /// 使能
    T_BUTTON_I _B_MOTION    = T_B_BTN1; /// 运动切换
    T_BUTTON_I _B_COOR      = T_B_BTN2; /// 坐标切换
}ST_B_INX;



/// 定义本次手柄数据
typedef struct ST_HANDLER_TYPE
{
    bool    criticalStop;  /// 紧急停止
    bool    enMotion;      /// 运动使能,true:en,false:dis
    bool    isPause;       /// 是否暂停状态
    bool    outSilk;       /// 送
    bool    inSilk;        ///
    bool    motionType;    /// 运动方式   true:XYZ   ----  false:OAT
    bool    coordinate;    /// 坐标系     true:BASE  ----  false:TOOL
    int     speed;         /// 运动速度
    UC2     x;
    UC2     y;
    UC2     z;
    UC2     a;
    UC2     b;
    UC2     c;
}ST_HD_T;


/// 获取按钮是否被按下了
inline bool GetButtonPressed(UC value,T_BUTTON_I index){
    return  (value >> index) & T_P_GET;
}


/// 返回2个字节的高位加低位
/// USB方式低位在前，232模式高位在前
inline UC2 HLDataCalc(UC i1,UC i2,bool isLH = false){
    if(isLH)
        return (UC2)((i2 << 8)+ i1);
    else {
        return (UC2)((i1 << 8)+ i2);
    }
}

/// UC2转换为Pos
inline double UC2ToPos(UC2 i2){
    return 1.00 * (i2 - T_P_3D_MID)  / T_P_3D_MID * T_P_ROBOT_RANGE ;
}

/// UC2转换为Percent
inline double UC2ToPercent(UC2 i2){
    return 0.01 * round((i2 - T_P_3D_MID) * 100  / T_P_3D_MID) ;
}

#endif // PROTOCOL_H
